<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<title>Exported from Notepad++</title>
<style type="text/css">
span {
	font-family: 'Courier New';
	font-size: 10pt;
	color: #000000;
}
.sc0 {
}
.sc1 {
}
.sc3 {
	font-weight: bold;
	color: #0000FF;
}
.sc4 {
	color: #8000FF;
}
.sc7 {
	font-weight: bold;
	color: #000080;
}
.sc8 {
	color: #FF8000;
}
.sc11 {
	color: #808080;
}
.sc12 {
	font-family: 'Comic Sans MS';
	font-size: 8pt;
	color: #008000;
}
</style>
</head>
<body>
<div style="float: left; white-space: pre; line-height: 1; background: #FFFFFF; "><span class="sc12">(* A compiler avec ocamlopt, puis a utiliser en pipe *)</span><span class="sc0">

</span><span class="sc3">open</span><span class="sc0"> </span><span class="sc1">Bplib</span><span class="sc0">
</span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">args</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">Array</span><span class="sc7">.</span><span class="sc1">to_list</span><span class="sc0"> </span><span class="sc1">Sys</span><span class="sc7">.</span><span class="sc1">argv</span><span class="sc7">;;</span><span class="sc0">
</span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">args_assoc</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">assoc_of_sys_argv</span><span class="sc0"> </span><span class="sc1">Sys</span><span class="sc7">.</span><span class="sc1">argv</span><span class="sc7">;;</span><span class="sc0">
</span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">get_arg_int_value</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">try_get_arg_int_value</span><span class="sc0"> </span><span class="sc1">args_assoc</span><span class="sc7">;;</span><span class="sc0">
</span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">get_arg_string_value</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">try_get_arg_string_value</span><span class="sc0"> </span><span class="sc1">args_assoc</span><span class="sc7">;;</span><span class="sc0">
</span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">_f</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">get_arg_int_value</span><span class="sc0"> </span><span class="sc11">"-f"</span><span class="sc7">;;</span><span class="sc0">

</span><span class="sc12">(* On peut instancier l'objet *)</span><span class="sc0">
</span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">pause</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc3">new</span><span class="sc0"> </span><span class="sc1">Bplib</span><span class="sc7">.</span><span class="sc1">sleeper</span><span class="sc0"> </span><span class="sc7">(</span><span class="sc8">1.</span><span class="sc7">/.(</span><span class="sc1">float_of_int</span><span class="sc0"> </span><span class="sc1">_f</span><span class="sc7">))</span><span class="sc0"> </span><span class="sc3">false</span><span class="sc7">;;</span><span class="sc0">

</span><span class="sc12">(* Il y a 2 possibilités : *)</span><span class="sc0">
</span><span class="sc12">(* 
   - Soit on est en mode -start avec changements durables
   - Soit on est en mode -default avec changements sur 1 cycle
*)</span><span class="sc0">

</span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">mode_start</span><span class="sc0"> </span><span class="sc1">start</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0">
  </span><span class="sc3">try</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">line</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc4">ref</span><span class="sc0"> </span><span class="sc1">start</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">thread_1</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0">
      </span><span class="sc3">while</span><span class="sc0"> </span><span class="sc3">true</span><span class="sc0"> </span><span class="sc3">do</span><span class="sc0">
    </span><span class="sc1">pause</span><span class="sc7">#</span><span class="sc1">sleep</span><span class="sc7">;</span><span class="sc0">
    </span><span class="sc1">print_endline</span><span class="sc0"> </span><span class="sc7">(!</span><span class="sc1">line</span><span class="sc7">)</span><span class="sc0">
      </span><span class="sc3">done</span><span class="sc0">
    </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">thread_2</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> 
      </span><span class="sc3">while</span><span class="sc0"> </span><span class="sc3">true</span><span class="sc0"> </span><span class="sc3">do</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">new_line</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">input_line</span><span class="sc0"> </span><span class="sc1">stdin</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc1">line</span><span class="sc0"> </span><span class="sc7">:=</span><span class="sc0"> </span><span class="sc1">new_line</span><span class="sc0">
      </span><span class="sc3">done</span><span class="sc0">
    </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc4">ignore</span><span class="sc7">(</span><span class="sc1">Thread</span><span class="sc7">.</span><span class="sc1">create</span><span class="sc0"> </span><span class="sc1">thread_1</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc7">);</span><span class="sc0">
    </span><span class="sc1">thread_2</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc0">
  </span><span class="sc3">with</span><span class="sc0">
  </span><span class="sc7">|</span><span class="sc0"> </span><span class="sc1">End_of_file</span><span class="sc0"> </span><span class="sc7">-&gt;</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc0">


</span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">mode_default</span><span class="sc0"> </span><span class="sc1">default</span><span class="sc0"> </span><span class="sc1">duree</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> 
  </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">q</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">Queue</span><span class="sc7">.</span><span class="sc1">create</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
  </span><span class="sc3">try</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">count</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc4">ref</span><span class="sc0"> </span><span class="sc8">0</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">line</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc4">ref</span><span class="sc0"> </span><span class="sc1">default</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">thread_1</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0">
      </span><span class="sc3">while</span><span class="sc0"> </span><span class="sc3">true</span><span class="sc0"> </span><span class="sc3">do</span><span class="sc0">
    </span><span class="sc1">incr</span><span class="sc7">(</span><span class="sc1">count</span><span class="sc7">);</span><span class="sc0">
    </span><span class="sc1">pause</span><span class="sc7">#</span><span class="sc1">sleep</span><span class="sc7">;</span><span class="sc0">
    </span><span class="sc3">if</span><span class="sc0"> </span><span class="sc7">!</span><span class="sc1">count</span><span class="sc0"> </span><span class="sc7">&lt;=</span><span class="sc0"> </span><span class="sc1">duree</span><span class="sc0">
    </span><span class="sc3">then</span><span class="sc0"> </span><span class="sc1">print_endline</span><span class="sc0"> </span><span class="sc7">(!</span><span class="sc1">line</span><span class="sc7">)</span><span class="sc0">
    </span><span class="sc3">else</span><span class="sc0"> 
      </span><span class="sc3">begin</span><span class="sc0">
        </span><span class="sc3">if</span><span class="sc0"> </span><span class="sc1">Queue</span><span class="sc7">.</span><span class="sc1">is_empty</span><span class="sc0"> </span><span class="sc1">q</span><span class="sc0">
        </span><span class="sc3">then</span><span class="sc0"> </span><span class="sc1">print_endline</span><span class="sc0"> </span><span class="sc1">default</span><span class="sc0">
        </span><span class="sc3">else</span><span class="sc0">
          </span><span class="sc3">begin</span><span class="sc0">
        </span><span class="sc1">line</span><span class="sc0"> </span><span class="sc7">:=</span><span class="sc0"> </span><span class="sc1">Queue</span><span class="sc7">.</span><span class="sc1">pop</span><span class="sc0"> </span><span class="sc1">q</span><span class="sc7">;</span><span class="sc0">
        </span><span class="sc1">count</span><span class="sc0"> </span><span class="sc7">:=</span><span class="sc0"> </span><span class="sc8">1</span><span class="sc7">;</span><span class="sc0">
        </span><span class="sc1">print_endline</span><span class="sc0"> </span><span class="sc7">(!</span><span class="sc1">line</span><span class="sc7">)</span><span class="sc0">
          </span><span class="sc3">end</span><span class="sc0">
      </span><span class="sc3">end</span><span class="sc0">
      </span><span class="sc3">done</span><span class="sc0">
    </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">thread_2</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> 
      </span><span class="sc3">while</span><span class="sc0"> </span><span class="sc3">true</span><span class="sc0"> </span><span class="sc3">do</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">new_line</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">input_line</span><span class="sc0"> </span><span class="sc1">stdin</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc1">Queue</span><span class="sc7">.</span><span class="sc1">push</span><span class="sc0"> </span><span class="sc1">new_line</span><span class="sc0"> </span><span class="sc1">q</span><span class="sc0">
      </span><span class="sc3">done</span><span class="sc0">
    </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc4">ignore</span><span class="sc7">(</span><span class="sc1">Thread</span><span class="sc7">.</span><span class="sc1">create</span><span class="sc0"> </span><span class="sc1">thread_1</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc7">);</span><span class="sc0">
    </span><span class="sc1">thread_2</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc0">
  </span><span class="sc3">with</span><span class="sc0">
  </span><span class="sc7">|</span><span class="sc0"> </span><span class="sc1">End_of_file</span><span class="sc0"> </span><span class="sc7">-&gt;</span><span class="sc0"> </span><span class="sc3">()</span><span class="sc0">

</span><span class="sc3">let</span><span class="sc0"> </span><span class="sc3">_</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0">
  </span><span class="sc3">if</span><span class="sc0"> </span><span class="sc1">List</span><span class="sc7">.</span><span class="sc1">mem</span><span class="sc0"> </span><span class="sc11">"-s"</span><span class="sc0"> </span><span class="sc1">args</span><span class="sc0">
  </span><span class="sc3">then</span><span class="sc0">
    </span><span class="sc12">(* Mode start *)</span><span class="sc0"> 
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">start</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">get_arg_string_value</span><span class="sc0"> </span><span class="sc11">"-s"</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc1">mode_start</span><span class="sc0"> </span><span class="sc1">start</span><span class="sc0">
  </span><span class="sc3">else</span><span class="sc0">
  </span><span class="sc3">if</span><span class="sc0"> </span><span class="sc1">List</span><span class="sc7">.</span><span class="sc1">mem</span><span class="sc0"> </span><span class="sc11">"-d"</span><span class="sc0"> </span><span class="sc1">args</span><span class="sc0">
  </span><span class="sc3">then</span><span class="sc0">
    </span><span class="sc12">(* Mode default *)</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">default</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">get_arg_string_value</span><span class="sc0"> </span><span class="sc11">"-d"</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">duree</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">get_arg_int_value</span><span class="sc0"> </span><span class="sc11">"-cy"</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc1">mode_default</span><span class="sc0"> </span><span class="sc1">default</span><span class="sc0"> </span><span class="sc1">duree</span><span class="sc0">
  </span><span class="sc3">else</span><span class="sc0">
    </span><span class="sc12">(* Mode default avec premiere ligne lue pour default *)</span><span class="sc0">
    </span><span class="sc1">Printf</span><span class="sc7">.</span><span class="sc1">fprintf</span><span class="sc0"> </span><span class="sc1">stderr</span><span class="sc0"> </span><span class="sc11">"Default Line : "</span><span class="sc7">;</span><span class="sc0"> </span><span class="sc1">flush</span><span class="sc0"> </span><span class="sc1">stderr</span><span class="sc7">;</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">default</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">input_line</span><span class="sc0"> </span><span class="sc1">stdin</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc3">let</span><span class="sc0"> </span><span class="sc1">duree</span><span class="sc0"> </span><span class="sc7">=</span><span class="sc0"> </span><span class="sc1">get_arg_int_value</span><span class="sc0"> </span><span class="sc11">"-cy"</span><span class="sc0"> </span><span class="sc3">in</span><span class="sc0">
    </span><span class="sc1">mode_default</span><span class="sc0"> </span><span class="sc1">default</span><span class="sc0"> </span><span class="sc1">duree</span><span class="sc0">
    
</span></div></body>
</html>
